#include "OCTTrackingApp.h"

#include <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QAction>
#include <unistd.h>

OCTTrackingApp::OCTTrackingApp(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
{
  ui = new Ui_MainWindow();
  ui->setupUi(this);
  
    //initializeUI(this);
  if (DEBUG)
    ui->textedit_info->setText("UI is initialized.");
    
    //cout << "UI is initialized." << endl;
  
  setupSlots(this);
  
  initializeTracking();
  if (DEBUG)
    cout << "Tracking object is initialized." << endl;
}

OCTTrackingApp::~OCTTrackingApp()
{
  //
  delete landmark1;
  delete landmark2;
  delete landmark3;
  delete landmark4;
  
  delete landmarkPattern;
  delete landmarkLocating;
  
  delete ui;
  delete octSource;
}

void OCTTrackingApp::initializeTracking()
{
  landmark1 = NULL;
  landmark2 = NULL;
  landmark3 = NULL;
  landmark4 = NULL;
  
  landmarkPattern = NULL;
  
  // create Landmark Pattern
  ui->droplist_pattern->addItem("Circular");
  ui->droplist_pattern->addItem("Cross");
  
  
  
  //landmarkPattern = new LandmarkPattern();
  
  ui->btn_landmark_1_clear->setEnabled(false);
  ui->btn_landmark_2_clear->setEnabled(false);
  ui->btn_landmark_3_clear->setEnabled(false);
  ui->btn_landmark_4_clear->setEnabled(false);
  ui->btn_landmark_1_set->setEnabled(true);
  ui->btn_landmark_2_set->setEnabled(true);
  ui->btn_landmark_3_set->setEnabled(true);
  ui->btn_landmark_4_set->setEnabled(true);
  
  
  /// load OCT image and shown in QT
  octSource = new OCTSourceSetting();
  initSourceImage = octSource->readSingleDatFile(
	  "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.08.22_Schritt14.dat", 512, 512);
  Mat tmpImg;
  
  // initialize OCTLandmarkLocating 
  landmarkLocating = new OCTLandmarkLocating();
  locationError = false;
  trackingError = false;
  
  normalize(initSourceImage, tmpImg, 0, 255, NORM_MINMAX, CV_8UC1);
  graySourceImage = tmpImg;
  cvtColor(tmpImg, colorSourceImage, CV_GRAY2RGB);
  cvtColor(tmpImg, colorTempImage, CV_GRAY2RGB);
  
  if (SHOWIMG)
  {
    namedWindow("Source Image", CV_WINDOW_AUTOSIZE);
    imshow("Source Image", tmpImg);
  }
  
  // Initialize the OCT and 4 landmarks images
  paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);
  landmarkBlackImage = Mat::zeros(120, 120, colorSourceImage.type());
  paintImage(landmarkBlackImage, ui->label_landmark_1, QImage::Format_RGB888);
  paintImage(landmarkBlackImage, ui->label_landmark_2, QImage::Format_RGB888);
  paintImage(landmarkBlackImage, ui->label_landmark_3, QImage::Format_RGB888);
  paintImage(landmarkBlackImage, ui->label_landmark_4, QImage::Format_RGB888);
}

void OCTTrackingApp::paintImage(Mat sourceImg, QLabel* target, QImage::Format format)
{
  /// Convert OpenCV Mat to QImage
  if (format == QImage::Format_Indexed8) 
  {
    QVector<QRgb> colorTable;
    for (int i=0; i<256; i++)
      colorTable.push_back(qRgb(i,i,i));
    QImage src = QImage(sourceImg.data, sourceImg.cols, sourceImg.rows, format);
    src.setColorTable(colorTable);
    
    target->setPixmap(QPixmap::fromImage(src));
  } 
  else if (format == QImage::Format_RGB888) 
  {
    QImage src = QImage(sourceImg.data, sourceImg.cols, sourceImg.rows, format);
    //cout << sourceImg.cols << endl;
    //cout << sourceImg.rows << endl;
    
    target->setPixmap(QPixmap::fromImage(src));
  }


}

void OCTTrackingApp::markPoint(Mat src, Point pt)
{
  line(src, Point(pt.x - 10, pt.y), Point(pt.x + 10, pt.y), Scalar(255,0,0) );
  line(src, Point(pt.x, pt.y - 10), Point(pt.x, pt.y + 10), Scalar(255,0,0) );
}

void OCTTrackingApp::markRegion(Mat src, Point pt, int range)
{
  rectangle(src, Point(pt.x - range, pt.y - range), Point(pt.x + range, pt.y + range), Scalar(255,255,0));
}



Landmark* OCTTrackingApp::createLandmark(Point center, int range, string fileName)
{
  return new Landmark(center, range, 
			     initSourceImage.colRange(center.x-range, center.x+range)
				    .rowRange(center.y-range, center.y+range), 
			     graySourceImage.colRange(center.x-range, center.x+range)
				    .rowRange(center.y-range, center.y+range),
			     fileName
			    ); 
}



void OCTTrackingApp::removeLandmark(Mat src, Point pt, int range)
{
    Mat mLandmark = src.colRange(pt.x - range - 1, pt.x + range + 1)
			.rowRange(pt.y - range - 1, pt.y + range + 1);
    
    Mat mUpdate =  colorTempImage.colRange(pt.x - range - 1, pt.x + range + 1)
				  .rowRange(pt.y - range - 1, pt.y + range + 1);
    
    mUpdate.copyTo(mLandmark);

}

void OCTTrackingApp::rePaintImage()
{
  colorTempImage.copyTo(colorSourceImage);
  paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);
}



bool OCTTrackingApp::validateLandmarkInput(QLineEdit* inputX, QLineEdit* inputY, QLineEdit* inputRange, int& x, int& y, int& r)
{
  QString inX = inputX->text();
  QString inY = inputY->text();
  QString inR = inputRange->text();
  
  x = inX.toInt();
  y = inY.toInt();
  r = inR.toInt();
  
  if (x >= 0 && x <= 512 && y >= 0 && y <= 512 && r >= 30 && r <= 50)
    return true;
  
  return false;
}


void OCTTrackingApp::writeInfo(const QString& text)
{
  ui->textedit_info->append(text);
}




/// Slots

void OCTTrackingApp::setupSlots(QMainWindow* MainWindow)
{
  connect (ui->btn_landmark_1_set, SIGNAL( clicked() ), this, SLOT( onLandmark1SetButtonClicked()) );
  connect (ui->btn_landmark_2_set, SIGNAL( clicked() ), this, SLOT( onLandmark2SetButtonClicked()) );
  connect (ui->btn_landmark_3_set, SIGNAL( clicked() ), this, SLOT( onLandmark3SetButtonClicked()) );
  connect (ui->btn_landmark_4_set, SIGNAL( clicked() ), this, SLOT( onLandmark4SetButtonClicked()) );
  
  connect (ui->btn_landmark_1_clear, SIGNAL( clicked() ), this, SLOT( onLandmark1ClearButtonClicked()) );
  connect (ui->btn_landmark_2_clear, SIGNAL( clicked() ), this, SLOT( onLandmark2ClearButtonClicked()) );
  connect (ui->btn_landmark_3_clear, SIGNAL( clicked() ), this, SLOT( onLandmark3ClearButtonClicked()) );
  connect (ui->btn_landmark_4_clear, SIGNAL( clicked() ), this, SLOT( onLandmark4ClearButtonClicked()) );
  
  connect(ui->btn_pattern_set, SIGNAL( clicked()), this, SLOT(onLandmarkPatternSetButtonClicked()) );
  connect(ui->btn_pattern_clear, SIGNAL(clicked()), this, SLOT(onLandmarkPatternClearButtonClicked()) );
 
  connect(ui->droplist_pattern, SIGNAL(currentIndexChanged(int)), this, SLOT(onLandmarkPatternListIndexChanged(int)));
  
  connect (ui->btn_control_ini, SIGNAL( clicked()), this, SLOT (onInitialLocatingButtonClicked()) );
  connect (ui->btn_control_start, SIGNAL( clicked()), this, SLOT (onStartTrackingButtonClicked()) );
}

void OCTTrackingApp::onLandmark1SetButtonClicked()
{
  int x, y, r;
  
  if (validateLandmarkInput(ui->lineedit_landmark_1_x, ui->lineedit_landmark_1_y, ui->lineedit_landmark_1_range, x, y, r))
  {
    // Save the landmark image   
    landmark1 = createLandmark(Point(x,y), r, "temp/landmark1.png");
       
    Mat landmarkImage;
    
    cvtColor(landmark1->getLandmarkImage(), landmarkImage, CV_GRAY2RGB);
    imwrite(landmark1->getFileName(), landmarkImage);
    
    // Load landmark image in QLabel
    ui->label_landmark_1->setPixmap(QPixmap(landmark1->getFileName().c_str(), 0, Qt::AutoColor));   
    
    // repaint the oct image frame
    markPoint(colorSourceImage, Point(x,y));
    markRegion(colorSourceImage, Point(x,y), r);
    paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);
    
    ui->btn_landmark_1_clear->setEnabled(true);
    ui->btn_landmark_1_set->setEnabled(false);
  } 
  else
  {
    QMessageBox::warning(this, "Error", ERROR_INPUT_INVALID);
  } 
}

void OCTTrackingApp::onLandmark1ClearButtonClicked()
{
  cout << "Delete Setting of Landmark 1" << endl;
  removeLandmark(colorSourceImage, landmark1->getLandmarkCenter(), landmark1->getLandmarkRange());
  paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);
  paintImage(landmarkBlackImage, ui->label_landmark_1, QImage::Format_RGB888);
  ui->lineedit_landmark_1_x->clear();
  ui->lineedit_landmark_1_y->clear();
  ui->lineedit_landmark_1_range->clear();
  
  ui->btn_landmark_1_clear->setEnabled(false);
  ui->btn_landmark_1_set->setEnabled(true);
  
  myDelete(landmark1);
}

void OCTTrackingApp::onLandmark2SetButtonClicked()
{
  int x, y, r;
  
  if (validateLandmarkInput(ui->lineedit_landmark_2_x, ui->lineedit_landmark_2_y, ui->lineedit_landmark_2_range, x, y, r))
  {
    // Save the landmark image   
    landmark2 = createLandmark(Point(x,y), r, "temp/landmark2.png");
    
    Mat landmarkImage;
    cvtColor(landmark2->getLandmarkImage(), landmarkImage, CV_GRAY2RGB);
    imwrite(landmark2->getFileName(), landmarkImage);
    
    // Load landmark image in QLabel
    ui->label_landmark_2->setPixmap(QPixmap(landmark2->getFileName().c_str(), 0, Qt::AutoColor));   
    
    // repaint the oct image frame
    markPoint(colorSourceImage, Point(x,y));
    markRegion(colorSourceImage, Point(x,y), r);
    paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);
    
    ui->btn_landmark_2_clear->setEnabled(true);
    ui->btn_landmark_2_set->setEnabled(false);
  } 
  else
  {
    QMessageBox::warning(this, "Error", ERROR_INPUT_INVALID);
  } 
}

void OCTTrackingApp::onLandmark2ClearButtonClicked()
{
  cout << "Delete Setting of Landmark 2" << endl;
  removeLandmark(colorSourceImage, landmark2->getLandmarkCenter(), landmark2->getLandmarkRange());
  paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);
  paintImage(landmarkBlackImage, ui->label_landmark_2, QImage::Format_RGB888);
  ui->lineedit_landmark_2_x->clear();
  ui->lineedit_landmark_2_y->clear();
  ui->lineedit_landmark_2_range->clear();
  
  ui->btn_landmark_2_clear->setEnabled(false);
  ui->btn_landmark_2_set->setEnabled(true);
  
  myDelete(landmark2);
}

void OCTTrackingApp::onLandmark3SetButtonClicked()
{
  int x, y, r;
  
  if (validateLandmarkInput(ui->lineedit_landmark_3_x, ui->lineedit_landmark_3_y, ui->lineedit_landmark_3_range, x, y, r))
  {
    // Save the landmark image
    
    landmark3 = createLandmark(Point(x,y), r, "temp/landmark3.png");
    
    Mat landmarkImage;
    cvtColor(landmark3->getLandmarkImage(), landmarkImage, CV_GRAY2RGB);
    imwrite(landmark3->getFileName(), landmarkImage);
    
    // Load landmark image in QLabel
    ui->label_landmark_3->setPixmap(QPixmap(landmark3->getFileName().c_str(), 0, Qt::AutoColor));   
    
    // repaint the oct image frame
    markPoint(colorSourceImage, Point(x,y));
    markRegion(colorSourceImage, Point(x,y), r);
    paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);
    
    ui->btn_landmark_3_clear->setEnabled(true);
    ui->btn_landmark_3_set->setEnabled(false);
  } 
  else
  {
    QMessageBox::warning(this, "Error", ERROR_INPUT_INVALID);
  } 
}

void OCTTrackingApp::onLandmark3ClearButtonClicked()
{
  cout << "Delete Setting of Landmark 3" << endl;
  removeLandmark(colorSourceImage, landmark3->getLandmarkCenter(), landmark3->getLandmarkRange());
  paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);
  paintImage(landmarkBlackImage, ui->label_landmark_3, QImage::Format_RGB888);
  ui->lineedit_landmark_3_x->clear();
  ui->lineedit_landmark_3_y->clear();
  ui->lineedit_landmark_3_range->clear();
  
  ui->btn_landmark_3_clear->setEnabled(false);
  ui->btn_landmark_3_set->setEnabled(true);
  
  myDelete(landmark3);
}

void OCTTrackingApp::onLandmark4SetButtonClicked()
{
  int x, y, r;
  
  if (validateLandmarkInput(ui->lineedit_landmark_4_x, ui->lineedit_landmark_4_y, ui->lineedit_landmark_4_range, x, y, r))
  {
    // Save the landmark image
    
    landmark4 = createLandmark(Point(x,y), r, "temp/landmark4.png");
    
    Mat landmarkImage;
    cvtColor(landmark4->getLandmarkImage(), landmarkImage, CV_GRAY2RGB);
    imwrite(landmark4->getFileName(), landmarkImage);
    
    // Load landmark image in QLabel
    ui->label_landmark_4->setPixmap(QPixmap(landmark4->getFileName().c_str(), 0, Qt::AutoColor));   
    
    // repaint the oct image frame
    markPoint(colorSourceImage, Point(x,y));
    markRegion(colorSourceImage, Point(x,y), r);
    paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);
    
    ui->btn_landmark_4_clear->setEnabled(true);
    ui->btn_landmark_4_set->setEnabled(false);
  } 
  else
  {
    QMessageBox::warning(this, "Error", ERROR_INPUT_INVALID);
  } 
}

void OCTTrackingApp::onLandmark4ClearButtonClicked()
{
  cout << "Delete Setting of Landmark 4" << endl;
  removeLandmark(colorSourceImage, landmark4->getLandmarkCenter(), landmark4->getLandmarkRange());
  paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);
  paintImage(landmarkBlackImage, ui->label_landmark_4, QImage::Format_RGB888);
  ui->lineedit_landmark_4_x->clear();
  ui->lineedit_landmark_4_y->clear();
  ui->lineedit_landmark_4_range->clear();
  
  ui->btn_landmark_4_clear->setEnabled(false);
  ui->btn_landmark_4_set->setEnabled(true);
  
  myDelete(landmark4);
}

void OCTTrackingApp::onInitialLocatingButtonClicked()
{
  int num = 0;
  locationError = false;
  
  if (landmark1 != NULL)
    num++;
  if (landmark2 != NULL)
    num++;
  if (landmark3 != NULL)
    num++;
  if (landmark4 != NULL)
    num++;
  
  if (num < 3 || landmarkPattern == NULL) {
    if (num < 3)
      QMessageBox::warning(this, "Error", ERROR_NO_LANDMARK);
    else
      QMessageBox::warning(this, "Error", ERROR_NO_LANDMARK_PATTERN);
  } else {
    ui->textedit_info->append("Start Initial Locating.");
    Point3f tmpPt;
    rePaintImage();
    
    if (landmark1 != NULL) {
      if (landmarkLocating->locatingLandmark(landmark1, landmarkPattern, tmpPt)) {
	cout << tmpPt << endl;
	
	// delete the old landmark and create new one
	int x = tmpPt.x;
	int y = tmpPt.y;
	int r = landmark1->getLandmarkRange();
	string filename = landmark1->getFileName();
	
	myDelete(landmark1);
	
	landmark1 = createLandmark(Point(x,y), r, filename);
	
	// repaint the landmark
	markPoint(colorSourceImage, Point(x, y));
	
	// reload landmark on landmark image
	ui->label_landmark_1->setPixmap(QPixmap(landmark1->getFileName().c_str(), 0, Qt::AutoColor));      
      
      } else {
	// can not locate landmark
	writeInfo("Can not locate landmark 1");
	locationError = true;
      }
      
    }
    
    if (landmark2 != NULL) {
      if (landmarkLocating->locatingLandmark(landmark2, landmarkPattern, tmpPt)) {
	cout << tmpPt << endl;
	
	// delete the old landmark and create new one
	int x = tmpPt.x;
	int y = tmpPt.y;
	int r = landmark2->getLandmarkRange();
	string filename = landmark2->getFileName();
	
	myDelete(landmark2);
	
	landmark2 = createLandmark(Point(x,y), r, filename);
	
	// repaint the landmark
	markPoint(colorSourceImage, Point(x, y));
	
	// reload landmark on landmark image
	ui->label_landmark_2->setPixmap(QPixmap(landmark2->getFileName().c_str(), 0, Qt::AutoColor));      
      
      } else {
	// can not locate landmark
	writeInfo("Can not locate landmark 2");
	locationError = true;
      }
    }    
    
    if (landmark3 != NULL) {
      if (landmarkLocating->locatingLandmark(landmark3, landmarkPattern, tmpPt)) {
	cout << tmpPt << endl;
	
	// delete the old landmark and create new one
	int x = tmpPt.x;
	int y = tmpPt.y;
	int r = landmark3->getLandmarkRange();
	string filename = landmark3->getFileName();
	
	myDelete(landmark3);
	
	landmark3 = createLandmark(Point(x,y), r, filename);
	
	// repaint the landmark
	markPoint(colorSourceImage, Point(x, y));
	
	// reload landmark on landmark image
	ui->label_landmark_3->setPixmap(QPixmap(landmark3->getFileName().c_str(), 0, Qt::AutoColor));      
      
      } else {
	// can not locate landmark
	writeInfo("Can not locate landmark 3");
	locationError = true;
      }
    }
    
    if (landmark4 != NULL) {
      if (landmarkLocating->locatingLandmark(landmark4, landmarkPattern, tmpPt)) {
	cout << tmpPt << endl;
	
	// delete the old landmark and create new one
	int x = tmpPt.x;
	int y = tmpPt.y;
	int r = landmark4->getLandmarkRange();
	string filename = landmark4->getFileName();
	
	myDelete(landmark4);
	
	landmark4 = createLandmark(Point(x,y), r, filename);
	
	// repaint the landmark
	markPoint(colorSourceImage, Point(x, y));
	
	// reload landmark on landmark image
	ui->label_landmark_4->setPixmap(QPixmap(landmark4->getFileName().c_str(), 0, Qt::AutoColor));      
      
      } else {
	// can not locate landmark
	writeInfo("Can not locate landmark 4");
	locationError = true;
      }
    }
  }
  
  if (locationError)
    QMessageBox::warning(this, "Error", ERROR_LANDMARK_LOCATING);
}


void OCTTrackingApp::onStartTrackingButtonClicked()
{
  /// OCT image file
  const char* octFile[] = {
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.16.03_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.51.05_Schritt17.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.44.55_Schritt16.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.53.52_Schritt17.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.55.54_Schritt17.dat",
/*			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.18.01_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.21.36_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.23.31_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.25.32_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.16.03_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.18.01_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.21.36_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.23.31_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.25.32_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.27.12_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.34.43_Schritt15.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.16.03_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.18.01_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.21.36_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.23.31_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.25.32_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.27.12_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.34.43_Schritt15.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.16.03_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.18.01_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.21.36_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.27.12_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.34.43_Schritt15.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.16.03_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.18.01_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.21.36_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.23.31_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.25.32_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.27.12_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.34.43_Schritt15.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.16.03_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.18.01_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.21.36_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.23.31_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.25.32_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.27.12_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.34.43_Schritt15.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.08.22_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.16.03_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.18.01_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.21.36_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.23.31_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.25.32_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.27.12_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.34.43_Schritt15.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.16.03_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.18.01_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.21.36_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.23.31_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.25.32_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.27.12_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.34.43_Schritt15.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.08.22_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.16.03_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.18.01_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.21.36_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.23.31_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.25.32_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.27.12_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.34.43_Schritt15.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.16.03_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.18.01_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.21.36_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.23.31_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.25.32_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.27.12_Schritt14.dat",
			    "/home/ding/Dropbox/OCTData/VTK_New/2012.05.22_18.34.43_Schritt15.dat",	*/		    
			    NULL
			    };
			    
  if (locationError) {
    QMessageBox::warning(this, "Error", ERROR_LANDMARK_LOCATING);
    
  } else {
    for (int i = 0; octFile[i] != NULL; i++) {
      cout << octFile[i] << endl;
      // Clear the old OCT image
      initSourceImage.release();
      graySourceImage.release();
      colorSourceImage.release();
      colorTempImage.release();
      
      // Read the new OCT image file
      initSourceImage = octSource->readSingleDatFile(octFile[i], 512, 512);
      Mat tmpImg;   
      
      normalize(initSourceImage, tmpImg, 0, 255, NORM_MINMAX, CV_8UC1);
      graySourceImage = tmpImg;
      cvtColor(tmpImg, colorSourceImage, CV_GRAY2RGB);
      cvtColor(tmpImg, colorTempImage, CV_GRAY2RGB);
      
      
      // repaint the OCT images
      paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);
      
      // create the new Landmarks in new OCT image
      if (landmark1 != NULL) {
	Point center = landmark1->getLandmarkCenter();
	int x = center.x;
	int y = center.y;
	int r = landmark1->getLandmarkRange();
	string filename = landmark1->getFileName();
	
	myDelete(landmark1);
	landmark1 = new Landmark(center, r, 
			      initSourceImage.colRange(x-r, x+r).rowRange(y-r, y+r), 
			      graySourceImage.colRange(x-r, x+r).rowRange(y-r, y+r),
			      filename
				); 
	
      }
      
      if (landmark2 != NULL) {
	Point center = landmark2->getLandmarkCenter();
	int r = landmark2->getLandmarkRange();
	int x = center.x;
	int y = center.y; 
	string filename = landmark2->getFileName();
	
	myDelete(landmark2);
	landmark2 = new Landmark(center, r, 
			      initSourceImage.colRange(x-r, x+r).rowRange(y-r, y+r), 
			      graySourceImage.colRange(x-r, x+r).rowRange(y-r, y+r),
			      filename
				); 
	
      }    
      
      if (landmark3 != NULL) {
	Point center = landmark3->getLandmarkCenter();
	int x = center.x;
	int y = center.y;      
	int r = landmark3->getLandmarkRange();
	string filename = landmark3->getFileName();
	
	myDelete(landmark3);
	landmark3 = new Landmark(center, r, 
			      initSourceImage.colRange(x-r, x+r).rowRange(y-r, y+r), 
			      graySourceImage.colRange(x-r, x+r).rowRange(y-r, y+r),
			      filename
				); 
	
      }    
      
      if (landmark4 != NULL) {
	Point center = landmark4->getLandmarkCenter();
	int x = center.x;
	int y = center.y;      
	int r = landmark4->getLandmarkRange();
	string filename = landmark4->getFileName();
	
	myDelete(landmark4);
	landmark4 = new Landmark(center, r, 
			      initSourceImage.colRange(x-r, x+r).rowRange(y-r, y+r), 
			      graySourceImage.colRange(x-r, x+r).rowRange(y-r, y+r),
			      filename
				); 
	
      } 
      
      // relocating Landmarks in new OCT image
      Point3f tmpPt;
      if (landmark1 != NULL) {
	if (landmarkLocating->locatingLandmark(landmark1, landmarkPattern, tmpPt)) {
	  cout << tmpPt << endl;
	  
	  // delete the old landmark and create new one
	  int x = tmpPt.x;
	  int y = tmpPt.y;
	  int r = landmark1->getLandmarkRange();
	  string filename = landmark1->getFileName();
	  
	  myDelete(landmark1);
	  
	  landmark1 = createLandmark(Point(x,y), r, filename);
	  
	  // repaint the landmark
	  markPoint(colorSourceImage, Point(x, y));
	  
	  // reload landmark on landmark image
	  ui->label_landmark_1->setPixmap(QPixmap(landmark1->getFileName().c_str(), 0, Qt::AutoColor));      
	
	} else {
	  // can not locate landmark
	  writeInfo("Can not locate landmark 1");
	  trackingError = true;
	}
	
      }
      
      if (landmark2 != NULL) {
	if (landmarkLocating->locatingLandmark(landmark2, landmarkPattern, tmpPt)) {
	  cout << tmpPt << endl;
	  
	  // delete the old landmark and create new one
	  int x = tmpPt.x;
	  int y = tmpPt.y;
	  int r = landmark2->getLandmarkRange();
	  string filename = landmark2->getFileName();
	  
	  myDelete(landmark2);
	  
	  landmark2 = createLandmark(Point(x,y), r, filename);
	  
	  // repaint the landmark
	  markPoint(colorSourceImage, Point(x, y));
	  
	  // reload landmark on landmark image
	  ui->label_landmark_2->setPixmap(QPixmap(landmark2->getFileName().c_str(), 0, Qt::AutoColor));      
	
	} else {
	  // can not locate landmark
	  writeInfo("Can not locate landmark 2");
	  trackingError = true;
	}
      }    
      
      if (landmark3 != NULL) {
	if (landmarkLocating->locatingLandmark(landmark3, landmarkPattern, tmpPt)) {
	  cout << tmpPt << endl;
	  
	  // delete the old landmark and create new one
	  int x = tmpPt.x;
	  int y = tmpPt.y;
	  int r = landmark3->getLandmarkRange();
	  string filename = landmark3->getFileName();
	  
	  myDelete(landmark3);
	  
	  landmark3 = createLandmark(Point(x,y), r, filename);
	  
	  // repaint the landmark
	  markPoint(colorSourceImage, Point(x, y));
	  
	  // reload landmark on landmark image
	  ui->label_landmark_3->setPixmap(QPixmap(landmark3->getFileName().c_str(), 0, Qt::AutoColor));      
	
	} else {
	  // can not locate landmark
	  writeInfo("Can not locate landmark 3");
	  trackingError = true;
	}
      }
      
      if (landmark4 != NULL) {
	if (landmarkLocating->locatingLandmark(landmark4, landmarkPattern, tmpPt)) {
	  cout << tmpPt << endl;
	  
	  // delete the old landmark and create new one
	  int x = tmpPt.x;
	  int y = tmpPt.y;
	  int r = landmark4->getLandmarkRange();
	  string filename = landmark4->getFileName();
	  
	  myDelete(landmark4);
	  
	  landmark4 = createLandmark(Point(x,y), r, filename);
	  
	  // repaint the landmark
	  markPoint(colorSourceImage, Point(x, y));
	  
	  // reload landmark on landmark image
	  ui->label_landmark_4->setPixmap(QPixmap(landmark4->getFileName().c_str(), 0, Qt::AutoColor));      
	
	} else {
	  // can not locate landmark
	  writeInfo("Can not locate landmark 4");
	  trackingError = true;
	}
	
	if (trackingError)
	  break;
      }    
      
      if (trackingError)
	writeInfo("Landmark locating error. Tracking Stopped.");
      else {
	paintImage(colorSourceImage, ui->label_octimage, QImage::Format_RGB888);  
	ui->label_octimage->repaint();
      }
      
      
      //sleep(1);
    }    
    
  }
			    
  
			    
}


void OCTTrackingApp::onLandmarkPatternSetButtonClicked()
{
  QString var = ui->lineedit_variable->text();
  int varInt = var.toInt();
    
  //cout << patternName << endl;
  if (varInt > 10 && varInt < 100) {
    // create landmark pattern
    landmarkPattern = new LandmarkPattern(patternName, varInt);
  } else {
    QMessageBox::warning(this, "Error", ERROR_LANDMARK_PATTERN_VARIABLE);
  }
    
}

void OCTTrackingApp::onLandmarkPatternClearButtonClicked()
{
  myDelete(landmarkPattern);
}


void OCTTrackingApp::onLandmarkPatternListIndexChanged(int idx)
{
  QString var = ui->droplist_pattern->itemText(idx);
  patternName = var.toStdString();
}



/// END SLOTS

#include "OCTTrackingApp.moc"
